package sword.chapter11BinarySearch;

/**
 * 求平方根
 *
 * m*m <= target && (m+1)*(m+1)> target m即平方根
 *
 * @author K
 * @date 2021-12-26 17:58
 */
public class S72MySqrt {
    public static int s72(int n) {
        int left = 1;
        int right = n;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (mid <= n / mid) {
                if (mid + 1 > n / (mid + 1)) {
                    return mid;
                }
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        int result = s72(18);
        System.out.println();
    }
}
